<?php
namespace app\index\controller;

use app\admin\model\User;
use app\common\model\Cash;
use app\common\model\Order;
use think\Session;

class UserController extends CommonController
{
    public $model = null; //模型实例
    protected $user_id = null; //用户 id
    //构造方法初始化模型
    public function __construct()
    {
        parent::__construct();
        if ( $this->model == null) $this->model = new User();
        if ( !$this->user_id ) $this->user_id = Session::get('user_id');
    }
    //1.前台首页
    public function index()
    {
        $user = $this->checkBind(false);
        $this->assign([
            'user'  =>  $user
        ]);
        return $this->fetch();
    }

    /**
     * 1. 签到 -- 页面
     */
    public function sign()
    {
        $user = $this->checkBind(); //检测支付宝绑定

        $this->assign([
            'user_id'  =>  $this->user_id,
            'user'  =>  $user,
        ]);
        return $this->fetch();
    }

    /**
     * 用户去签到的 ajax 实现方法
     */
    public function toSign()
    {
        if (request()->isPost())
        {
            $data = input('post.');
            if ( empty($data['user_id']) || ($user_id = Session::get('user_id') != $data['user_id']) )
                return $this->error('签到失败');
            $rs = $this->model->sign($data['user_id']);
            if (is_string($rs))
            {
                return $this->error($rs);
            }
            else if ($rs !== false)
            {
                return $this->success('签到成功');
            }
            else
            {
                return $this->error( '签到失败，请重试');
            }
        }
    }

    /**
     * 用户去抽奖的 ajax 实现方法
     */
    public function toLottery()
    {
        if (request()->isPost())
        {
            //$user_id = Session::get('user_id');
            $data = input('post.');
            if ( empty($data['user_id']) )
                return $this->error('抽奖失败');
            $rs = $this->model->lottery($data['user_id']);
            if (is_string($rs))
            {
                return $this->error($rs);
            }
            else if ($rs !== false)
            {
                return $this->success('抽奖成功');
            }
            else
            {
                return $this->error( '抽奖失败，请重试');
            }
        }
    }

    public function gift()
    {
        echo 1;
    }

    /**
     * 2. 推荐人账号 及 绑定操作
     */
    public function referee()
    {
        //1. 先查询用户是否已经绑定支付宝
        $user = $this->checkBind(false);
        //if (!empty($user['referee']))
            //return $this->redirect(url('/user'));
        if (request()->isPost())
        {
            $data = input('post.referee');
            if (empty($data))
                return $this->error('账号不能为空');
            $rs = $this->model->bindReferee($this->user_id, $data);
            //var_dump($rs);exit();
            if (is_string($rs))
            {
                return $this->error($rs);
            }
            else if ($rs !== false)
            {
                return $this->success('绑定账号成功', url('/user'));
            }
            else
            {
                return $this->error( '绑定失败，请重试');
            }
        }
        $this->assign([
           'data'   =>  $user,
        ]);
        return $this->fetch();
    }
    
    /**
     * 3.绑定支付宝 页面
     */
    public function bind()
    {
        //1. 先查询用户是否已经绑定支付宝
        $user = $this->checkBind(false);
        //if (!empty($user['alipay']))
            //return $this->redirect(url('/user'));
        if (request()->isPost())
        {
            $data = input('post.alipay');
            if (empty($data))
                return $this->error('支付宝账号不能为空');
            $rs = $this->model->bindAlipay($this->user_id, $data);
            //var_dump($rs);exit();
            if (is_string($rs))
            {
                return $this->error($rs);
            }
            else if ($rs !== false)
            {
                return $this->success('绑定支付宝成功', url('/user'));
            }
            else
            {
                return $this->error( '绑定失败，请重试');
            }
        }
        $this->assign([
            'data'   =>  $user,
        ]);
        return $this->fetch();
    }

    /**
     * 5.充值 页面
     */
    public function recharge()
    {
        if (request()->isPost())
        {
            $data = input('post.');
            $rs = (new Order())->data_verify($data);
            if ($rs === true) {
                $this->success('提交成功，审核通过即可入账', url('/user'));
            }else {
                $this->error($rs);
            }
        }
        $user = $this->checkBind(); //检测支付宝绑定
        $this->assign([
            'user'   =>  $user,
        ]);
        return $this->fetch();
    }

    /**
     * 6.提现 页面
     */
    public function cash()
    {
        $user = $this->checkBind(); //检测支付宝绑定
        if (request()->isPost())
        {
            $data = input('post.');
            //1. 先判断资金是否大于提现金额
            if ($user['recharge_money']+$user['profit'] < $data['money'])
                $this->error('可提现资金余额不足');
            $rs = (new Cash())->data_verify($data);
            if ($rs === true) {
                $this->success('提交成功，审核通过即可到账', url('/user'));
            }else {
                $this->error($rs);
            }
        }

        $this->assign([
            'user'   =>  $user,
        ]);
        return $this->fetch();
    }

    /**
     * 检测用户是否已经绑定支付宝账号, 并返回用户信息
     * @param bool $check
     * @return array|bool|false|\PDOStatement|string|\think\Model|void
     */
    public function checkBind($check = true)
    {
        $user = $this->model->getById($this->user_id); //用户信息
        if ($check && empty($user['alipay'])) return $this->redirect(url('/bind'));
        return $user;
    }

}
